var redis = require("redis");
var async = require('async');
var path = require('path')
var fs = require('fs')
var config = require('../config')
var logger = require('../logger')

function log() {
    logger.log(arguments);
}


//往数据库加入一条文件id和tag的对应关系数据
function addtag(client, fileid, maintag, childtag, grandsontag, func) {
    async.waterfall([
        (cb) => {
            client.sadd(fileid + config.tags, grandsontag, (err, response) => {
                log('sadd', fileid + config.tags, grandsontag, err, response);
                cb(err);
            })
        },
        (cb) => {
            let tables = maintag + '.' + childtag + '.' + grandsontag + config.files;
            client.sadd(tables, fileid, (err, response) => {
                log('sadd', tables, fileid, err, response);
                cb(err);
            })
        }
    ], (err) => {
        func(err);
    })
}

function getfileinfo(client, fileid, cb) {
    async.waterfall([
        (callback) => {
            client.get(fileid, (err, response) => {
                callback(err, response);
            })
        },

        (response, callback) => {
            if (response) {
                var info = JSON.parse(response);
                if (info) {
                    info.id = fileid;
                    callback(null, info);
                } else {
                    callback(fileid + 'file infomation is not json format', null);
                }
            } else {
                callback(fileid + 'file not found in database', null);
            }
        },
    ], (err, info) => {
        cb(err, info);
    })
}

function readfile(client, resourcepath, fileid, cb) {
    async.waterfall([
        (callback) => {
            getfileinfo(client, fileid, (err, info) => {
                callback(err, info);
            })
        },

        (info, callback) => {
            if (info) {
                var fullname = path.join(resourcepath, fileid) + info.suffix;
                callback(null, fullname, info.name);
            } else {
                callback('file not found');
            }
        },
    ], (err, fullname, suffix) => {
        if (err) {
            return cb(err);
        }
        fs.readFile(fullname, (e, data) => {
            return cb(e, data, suffix);
        });
    })
}

function getallmaitags(client, cb) {
    client.smembers(config.allmaintag, (err, response) => {
        cb(err, response);
    })
}

function getfiletags(client, fileid, cb) {
    client.smembers(fileid + config.tags, (err, response) => {
        cb(err, response);
    })
}

function setfileinfo(client, fileid, info, cb) {
    var content = JSON.stringify(info);
    client.set(fileid, content, (err, response) => {
        cb(err);
    })
}

function getchildtagtree(client, maintag, cb) {
    async.waterfall([
        (callback) => {
            client.smembers(maintag + config.childtags, (err, response) => {
                callback(err, response);
            });
        },

        (childtags, callback) => {
            var tree = {};
            async.forEach(childtags, (childtag, cb) => {
                let table = maintag + '.' + childtag + config.grandsontags;
                client.smembers(table, (err, response) => {
                    if (err) {
                        cb(err)
                    } else {
                        tree[childtag] = response;
                        cb(err);
                    }
                });
            }, (err) => {
                callback(err, tree);
            })
        },
    ], (err, tree) => {
        cb(err, tree);
    });
}

function gettagtree(client, cb) {
    var tree = {};
    async.waterfall([
        //获取所有一级标签
        (callback) => {
            getallmaitags(client, (err, response) => {
                callback(err, response);
            });
        },

        //获取所有的childtags
        (maintags, callback) => {
            async.forEach(maintags, (maintag, cb) => {
                getchildtagtree(client, maintag, (err, childtree) => {
                    if (err) {
                        cb(err);
                    } else {
                        tree[maintag] = childtree;
                        cb(null);
                    }
                })
            }, (err) => {
                callback(err);
            })
        },

    ], (err) => {
        cb(err, tree);
    });
}


module.exports = {
    addtag,
    getfileinfo,
    readfile,
    getallmaitags,
    getfiletags,
    setfileinfo,
    gettagtree,
}